<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 数字累加
        // var arr = [1, 2, 3, 4];
        // var res = arr.reduce(function(pre, current, index) {
        //     console.log(pre, current, index);
        //     return pre+current;
        // },0)
        // console.log(res);
        // 二、数组的累加操作
        // var arr = [1,2,3,4];
        // var res =  arr.reduce(function(pre,current){
        //     pre.push(current+10);
        //     return pre;
        // },[]);
        // console.log(res);

        // 1.通过reduce 给数组去重复 
        // var arr = [1,2,1,3,2,4,3,5,5];
        // var res =  arr.reduce(function(pre, current) {
        //     pre.indexOf(current) === -1 ? pre.push(current) : null;
        //     return pre;
        // },[])
        // console.log(res);
        // 2.var str = "abcabccaaa";  ----> {a:5,b:2,c:3}

        // 对象的键名具有唯一性
        var arr = [1,2,1,3,2,4,3,5,5];
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
            typeof obj[arr[i]] === 'undefined' ? obj[arr[i]] = 1: obj[arr[i]]++;
        }
        console.log(obj);
    </script>
</body>
</html>